// pages/tools/statistical-calculator/statistical-calculator.js
function Pair(x, y) {
    this.x = x;
    this.y = y;
}

function Content() {
    this.pairs = [];
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: [],
        result: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            content: new Content(),
            result: {
                a: {
                    key: "a",
                    value: 0,
                    index: 1,
                }, b: {
                    key: "b",
                    value: 0,
                    index: 2,
                }, r: {
                    key: "r",
                    value: 0,
                    index: 3,
                }, xave: {
                    key: "x的平均值",
                    value: 0,
                    index: 4,
                }, xsum: {
                    key: "Σx",
                    value: 0,
                    index: 5,
                }, xsqsum: {
                    key: "Σ(x^2)",
                    value: 0,
                    index: 6,
                }, sxsq: {
                    key: "(sx)^2",
                    value: 0,
                    index: 7,
                }, sx: {
                    key: "sx",
                    value: 0,
                    index: 8,
                }, n: {
                    key: "n",
                    value: 0,
                    index: 9,
                }, yave: {
                    key: "y的平均值",
                    value: 0,
                    index: 10,
                }, ysum: {
                    key: "Σy",
                    value: 0,
                    index: 11,
                }, ysqsum: {
                    key: "Σ(y^2)",
                    value: 0,
                    index: 12,
                }, sysq: {
                    key: "(sy)^2",
                    value: 0,
                    index: 13,
                }, sy: {
                    key: "sy",
                    value: 0,
                    index: 14,
                }, xysum: {
                    key: "Σxy",
                    value: 0,
                    index: 15,
                }, xcubsum: {
                    key: "Σ(x^3)",
                    value: 0,
                    index: 16,
                }, xsqysum: {
                    key: "Σ(x^2*y)",
                    value: 0,
                    index: 17,
                }, xsqsqsum: {
                    key: "Σ(x^4)",
                    value: 0,
                    index: 18,
                },
            },
        });
        let content = this.data.content;
        content.pairs.push(new Pair(0, 0));
        this.setData({
            content: content,
        });
    },
    onShareAppMessage: function (res) {
        return {
          title: '快来看看学长开发的万能基物实验小程序！',
          path: 'pages/index/index',
          imageUrl: 'https://s1.ax1x.com/2020/07/25/aSICY4.jpg',
          success: function (res) {
            console.log('成功分享', res)
          }
        }
      },

    addItem: function() {
        let content = this.data.content;
        content.pairs.push(new Pair(0, 0));
        this.setData({
            content: content,
        });
    },

    rmItem: function() {
        if (this.data.content.pairs.length >= 2) {
            let content = this.data.content;
            content.pairs.pop();
            this.setData({
                content: content,
            });
        }
    },

    calculate: function() {
        let content = this.data.content;
        let result = this.data.result;
        /* Parse the input data */
        for (i = 0; i < content.pairs.length; i++) {
            let x = parseFloat(content.pairs[i].x);
            let y = parseFloat(content.pairs[i].y);
            if (isNaN(x) || isNaN(y)) {
                wx.showToast({
                  title: '请输入数字！',
                  icon: 'none',
                });
                return;
            }
            content.pairs[i].x = x;
            content.pairs[i].y = y;
        }
        /* calculating */
        let aveOfX = 0.0;
        let sumOfX = 0.0;
        let sumOfXsq = 0.0;
        let sumOfXcub = 0.0;
        let sumOfXsqsq = 0.0;
        let aveOfY = 0.0;
        let sumOfY = 0.0;
        let sumOfYsq = 0.0;
        let sumOfXY = 0.0;
        let sumOfXXY = 0.0;
        let sx2 = 0.0;
        let sx = 0.0;
        let sy2 = 0.0;
        let sy = 0.0;
        for (i = 0; i < content.pairs.length; i++) {
            let x = content.pairs[i].x;
            let y = content.pairs[i].y;
            sumOfX = sumOfX + x;
            sumOfXsq = sumOfXsq + x * x;
            sumOfXcub = sumOfXcub + x * x * x;
            sumOfXsqsq = sumOfXsqsq + x * x * x * x;
            sumOfY = sumOfY + y;
            sumOfYsq = sumOfYsq + y * y;
            sumOfXY = sumOfXY + x * y;
            sumOfXXY = sumOfXXY + x * x * y;
        }
        let n = content.pairs.length;
        let cross = 0.0;
        aveOfX = sumOfX / n;
        aveOfY = sumOfY / n;
        for (i = 0; i < content.pairs.length; i++) {
            let x = content.pairs[i].x;
            let y = content.pairs[i].y;
            sx2 = sx2 + (x - aveOfX) * (x - aveOfX);
            sy2 = sy2 + (y - aveOfY) * (y - aveOfY);
            cross = cross + (x - aveOfX) * (y - aveOfY);
        }
        let a = 0.0;
        let b = 0.0;
        let r = 0.0;
        if (n == 1) {
            a = 0.0;
            b = aveOfY;
            r = 0.0;
            sx2 = 0.0;
            sx = 0.0;
            sy2 = 0.0;
            sy = 0.0;
        } else {
            a = (n * sumOfXY - sumOfX * sumOfY) / (n * sumOfXsq - sumOfX * sumOfX);
            b = aveOfY - a * aveOfX;
            r = cross / Math.sqrt(sx2) / Math.sqrt(sy2);
            sx2 = sx2 / (n - 1);
            sx = Math.sqrt(sx2);
            sy2 = sy2 / (n - 1);
            sy = Math.sqrt(sy2);
        }
        /* update the result */
        result.a.value = a;
        result.b.value = b;
        result.r.value = r;
        result.xave.value = aveOfX;
        result.xsum.value = sumOfX;
        result.xsqsum.value = sumOfXsq;
        result.sxsq.value = sx2;
        result.sx.value = sx;
        result.n.value = n;
        result.yave.value = aveOfY;
        result.ysum.value = sumOfY;
        result.ysqsum.value = sumOfYsq;
        result.sysq.value = sy2;
        result.sy.value = sy;
        result.xysum.value = sumOfXY;
        result.xcubsum.value = sumOfXcub;
        result.xsqysum.value = sumOfXXY;
        result.xsqsqsum.value = sumOfXsqsq;
        this.setData({
            content: content,
            result: result,
        });
    },

    onXInput: function(e) {
        let index = parseInt(e.currentTarget.id.replace("x", ""));
        let value = e.detail.value;
        let content = this.data.content;
        this.data.content.pairs[index].x = value;
        this.setData({
            content: content,
        });
    },

    onYInput: function(e) {
        let index = parseInt(e.currentTarget.id.replace("y", ""));
        let value = e.detail.value;
        let content = this.data.content;
        this.data.content.pairs[index].y = value;
        this.setData({
            content: content,
        });
    },
})